package net.hymnal.church;

import java.util.List;
import java.util.Map;

import net.hymnal.church.HymnsManager.ThemesTree;
import net.hymnal.church.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ThemesActivity extends Activity{
	ListView mListView; 
	TextView mTextView;
	static enum Stage{
		MAIN_THEME, SUB_THEME, NUM_HYMNS
	}
	
	static Stage currentStage = Stage.MAIN_THEME; 
	   
	@Override
	public void onCreate(Bundle savedInstanceState) { 
		setTheme(GimnsMain.mSettings.getCurrentTheme());
		super.onCreate(savedInstanceState);   
		
		setContentView(R.layout.tree_themes);
		initControls();
	}
	
	void initControls() {		
		int bgColorTitle = -1;
		int textColorNumber = -1;
		mTextView = (TextView)findViewById(R.id.text_title_tree);
		mListView = (ListView)findViewById(R.id.list_view_tree);
		
		if(HymnActivity.BLACK) {		    
		    bgColorTitle = Settings.BackgoundColor.BACK_GROUND_BLACK_TITLE;
		    textColorNumber = Color.BLUE;
		} else {						
			bgColorTitle = Settings.BackgoundColor.BACK_GROUND_WHITE_TITLE;
			textColorNumber = Color.LTGRAY;
		}
		HymnsManager.ThemesTree inst = HymnsManager.ThemesTree.getInstance();
		if(currentStage == Stage.MAIN_THEME) {			
			mTextView.setText(R.string.main_theme_text);
			mTextView.setBackgroundColor(bgColorTitle);
			mTextView.setTextColor(textColorNumber);
			int layoutID = Settings.getOneLinesRowLayout(this);
		    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		    		layoutID, android.R.id.text1, inst.getMainThemesArray());
		    mListView.setAdapter(adapter);
		    mListView.setOnItemClickListener(listener);		    
		    return;
		}
		if(currentStage == Stage.SUB_THEME) {
			mTextView.setText(HymnsManager.ThemesTree.seletedMainTheme);
			mTextView.setBackgroundColor(bgColorTitle);
			mTextView.setTextColor(textColorNumber);
			//android.R.layout.simple_list_item_2
			int layoutID = Settings.getTwoLinesRowLayout(this);
			SimpleAdapter adapter = new SimpleAdapter(this, inst.createSubItemList(), layoutID, 
		            new String[] {HymnsManager.ThemesTree.LINE_TOP_NAME, HymnsManager.ThemesTree.LINE_BOTTOM_NAME},
		            new int[] {android.R.id.text1, android.R.id.text2});
		        
			mListView.setAdapter(adapter);			
			mListView.setOnItemClickListener(listener);			
			return;
		}
		if(currentStage == Stage.NUM_HYMNS) {
			mTextView.setText(HymnsManager.ThemesTree.seletedMainTheme + "\n" + HymnsManager.ThemesTree.seletedSubTheme);
			mTextView.setBackgroundColor(bgColorTitle);
			mTextView.setTextColor(textColorNumber);
			int array[] = inst.getLastSelectedVector();
			int size = array.length;
			String arrayStr[] = new String[size];
			for(int i = 0; i < size; i++) {
				int num = array[i];
				SingleHymn h = HymnsManager.getSingleHymn(num - 1);
				arrayStr[i] = Integer.toString(num) + " " + h.getFirstLine();				
			}
			int layoutID = Settings.getOneLinesRowLayout(this);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					layoutID, android.R.id.text1, arrayStr);
			mListView.setAdapter(adapter);
			mListView.setOnItemClickListener(listener);
			return;
		}
	}
	 
	OnItemClickListener listener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long id) {			
			startNextActivity(view, (int)id);				
		}
	};
	
	void startNextActivity(View v, int id) {
		ThemesTree inst = ThemesTree.getInstance();
		if(ThemesActivity.currentStage == Stage.MAIN_THEME) {
			inst.seletSubThemesArray(id);
			currentStage = Stage.SUB_THEME;
			Intent myIntent = new Intent(v.getContext(),
					ThemesActivity.class);
			startActivityForResult(myIntent, 0);
		} else if(ThemesActivity.currentStage == Stage.SUB_THEME) {
			inst.selectVectorHymnsNumbers(id);
			currentStage = Stage.NUM_HYMNS;
			Intent myIntent = new Intent(v.getContext(),
					ThemesActivity.class);
			startActivityForResult(myIntent, 0); 
		} else if(ThemesActivity.currentStage == Stage.NUM_HYMNS) {
			int array[] = inst.getLastSelectedVector();
			int hymnNumber = array[id];
			HymnActivity.startHymnActivity(this, v, hymnNumber-1);
		}						
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        if(currentStage == Stage.NUM_HYMNS) {
	        	currentStage = Stage.SUB_THEME;
	        } else if(currentStage == Stage.SUB_THEME) {
	        	currentStage = Stage.MAIN_THEME;
	        }
	    }
	    return super.onKeyDown(keyCode, event);
	}
}
